#include <iostream>
#include <cstring>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

struct ClientData
{
    int sockfd_;
    struct sockaddr_in client_;
};

void Usage(std::string proc)
{
    std::cout << "\n\rUsage: " << proc << " serverip serverport\n"
              << std::endl;
}

enum
{
    SOCKETERR = 1,
    CONNECTERR,
    READERR,
    WRITEERR
};

void *sendmessage(void *args)
{
    ClientData *client = static_cast<ClientData *>(args);
    socklen_t len = sizeof(client->client_);
    while (true)
    {
        std::string message;
        std::cout << "Please Enter# ";
        getline(std::cin, message);
        write(client->sockfd_, message.c_str(), message.size());
    }
}

void *recemessage(void *args)
{

    ClientData *client = static_cast<ClientData *>(args);
    socklen_t len = sizeof(client->client_);
    while (true)
    {
        char buffer[4096];
        int n = read(client->sockfd_, buffer, sizeof(buffer));
        if (n > 0)
        {
            buffer[n] = 0;
            std::cout << buffer << std::endl;
        }
    }
    return nullptr;
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        exit(0);
    }
    std::string ip = argv[1];
    uint16_t port = std::stoi(argv[2]);

    struct sockaddr_in local;
    local.sin_port = htons(port);
    local.sin_family = AF_INET;
    char buffer[1024];
    // inet_pton(AF_INET,buffer,&(local.sin_addr));

    inet_aton(ip.c_str(), &(local.sin_addr));

    while (true)
    {
        int listensockfd;
        listensockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (listensockfd < 0)
        {
            std::cerr << "socket error" << std::endl;
            exit(SOCKETERR);
        }
        socklen_t len = sizeof(local);
        pthread_t sendtid, recetid;
        ClientData *client = new ClientData();
        client->client_ = local;
        client->sockfd_ = listensockfd;
        bool isreconnect = true;
        int cnt = 5;
        do
        {
            std::cout << "begin connet" << std::endl;
            int n = connect(client->sockfd_, (sockaddr *)(&client->client_), len);
            std::cout << n << std::endl;
            if (n < 0)
            {
                isreconnect = true;
                cnt--;
                std::cerr << "connect error..., reconnect: " << cnt << std::endl;
            }
            else
            {
                std::cerr << "connect success " << cnt << std::endl;
                break;
            }
        } while (cnt && isreconnect);
        while (true)
        {
            std::string message;
            std::cout << "Please Enter# ";
            getline(std::cin, message);
            write(client->sockfd_, message.c_str(), message.size());
            char buffer[4096];
            int n = read(client->sockfd_, buffer, sizeof(buffer));
            if (n > 0)
            {
                buffer[n] = 0;
                std::cout << buffer << std::endl;
            }
        }
        // pthread_create(&sendtid, nullptr, sendmessage, client);
        // pthread_create(&recetid, nullptr, recemessage, client);
        // pthread_join(sendtid, nullptr);
        // pthread_join(recetid, nullptr);
        close(listensockfd);
    }
}